//
//  HTUnbindCopilotCell.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/26.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTUnbindCopilotCell.h"
#import "UIImageView+WebCache.h"
#import "UIAlertController+Constructor.h"

@interface HTUnbindCopilotCell()
@property (weak, nonatomic) IBOutlet UIImageView *copilotImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *unBindButton;

@end

@implementation HTUnbindCopilotCell
- (void)setUnbindModel:(HTUnbindCopilotModel *)unbindModel{
    _unbindModel = unbindModel;
    self.nameLabel.text = unbindModel.name;
    self.phoneLabel.text = unbindModel.phone;
     [self.copilotImageView sd_setImageWithURL:[NSURL URLWithString:unbindModel.picurl] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.copilotImageView.layer.masksToBounds = YES;
    self.copilotImageView.layer.cornerRadius = 22;
    
}
- (IBAction)didUnbindBtn:(id)sender {
    
    [UIAlertController alertStyleShowInController:self.controller  title:@"" message:[NSString stringWithFormat:@"确认将与你的副驾：%@\n%@ 解绑吗？",self.nameLabel.text,self.phoneLabel.text] alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
        [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self netLoadUnbind];
        }]];
    } completion:^{
        
    }];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark --- 网络请求
- (void)netLoadUnbind{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    
    [HTHttpRequestManager requestUnbindCopilotsWithID:self.unbindModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"解绑：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"解绑失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
          [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        if (self.detialBlock) {
            self.detialBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
          [HTShowUtil showMessage:@"解绑失败"];
    }];
}

@end


